package com.tedu.assess.service.impl;

import com.tedu.assess.mapper.AssessmentMapper;
import com.tedu.assess.mapper.HealthMapper;
import com.tedu.assess.pojo.dto.AssessmentParam;
import com.tedu.assess.pojo.dto.HealthParam;
import com.tedu.assess.pojo.entity.Assess;
import com.tedu.assess.pojo.entity.Health;
import com.tedu.assess.pojo.vo.AssessVO;
import com.tedu.assess.pojo.vo.HealthVO;
import com.tedu.assess.service.AssessmentService;
import com.tedu.user.mapper.UserMapper;
import com.tedu.user.pojo.dto.UserQuery;
import com.tedu.user.pojo.vo.UserVO;
import com.tedu.user.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Transactional
@Slf4j
@Service
public class AssessmentServiceImpl implements AssessmentService {

    @Autowired
    AssessmentMapper assessmentMapper;
    @Autowired
    HealthMapper healthMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public void insert(AssessmentParam assessmentParam) {
        log.debug("保存评估信息业务:参数={}", assessmentParam);
        Assess assess = new Assess();
        BeanUtils.copyProperties(assessmentParam, assess);
        assess.setAssessmentDate(new Date());
        assessmentMapper.insert(assess);
        UserQuery userQuery = new UserQuery();
        userQuery.setUsername(assess.getUsername());
        List<UserVO> userVO= userMapper.selectUser(userQuery);
        HealthParam healthParam = new HealthParam();
        //通过user接口新增性别，身份证，年龄
        healthParam.setAge(userVO.get(0).getAge());
        healthParam.setNum(userVO.get(0).getNum());
        healthParam.setGender(userVO.get(0).getGender());
        //通过health接口新增评估时间，评估结果，评估状态，评估人
        healthParam.setUsername(assess.getUsername());
        healthParam.setAssessmentResult(assess.getAssessmentResult());
        healthParam.setAssessmentState("10");
        healthParam.setAssessmentTime(new Date());
        healthParam.setEmpId(assess.getEmpId());
        Health health = new Health();
        BeanUtils.copyProperties(healthParam,health);
        healthMapper.insert(health);
    }

    @Override
    public AssessVO selectDetail(Integer id) {
        log.debug("查询健康详情业务:参数{}", id);
        HealthVO healthVO = healthMapper.selectById(id);
        System.out.println(healthVO);
        AssessVO assessVO =assessmentMapper.selectDetail(healthVO.getUsername());
        System.out.println(assessVO);
        return assessVO;
    }
}
